﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    public GameObject TapNote;
    public GameObject HoldNote;
    private string[] noteDetail;
    private string[] formerDetail;//保存上一个音符的信息 用于多押调整错位
    private float offsetY;//用于多押调整错位的偏移量
    private int noteNum = 1;
    private float BPM;
    private float noteSpeed;
    private float noteDistance;//音符到判定线的距离
    private float noteScale; //note适应分辨率缩放比例
    private string scoreString;//字符串型分数 在前面补0之后用于分数text的显示
    public GameObject audioTrack;
    private GameObject tapNote;
    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else if (Instance != null)
        {
            Destroy(gameObject);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        noteScale = (GlobalSettings.Instance.standardWidth / GlobalSettings.Instance.standardHeight) / ((float)Screen.width / (float)Screen.height);
    }

    // Update is called once per frame
    void Update()
    {
        if(GlobalSettings.Instance.isPaused==false)
        {
            if (GlobalSettings.Instance.isStarted == true && GlobalSettings.Instance.sheetEnd == false)
            {
                if(noteNum > 1)
                {
                    formerDetail = GlobalSettings.Instance.notes[noteNum - 1].Split(',');
                }
                noteDetail = GlobalSettings.Instance.notes[noteNum].Split(',');
                BPM = float.Parse(noteDetail[1]);
                noteSpeed = GlobalSettings.Instance.streamMultiply * BPM / 60f; //音符算上流速之后实际速度
                noteDistance = GlobalSettings.Instance.topPosY - GlobalSettings.Instance.k1Pos.y;
                if (Time.time + noteDistance / noteSpeed + GlobalSettings.Instance.offset - GlobalSettings.Instance.startTime >= float.Parse(noteDetail[0]))
                {
                    if(noteNum > 1)
                    {
                        if (formerDetail[0] == noteDetail[0])
                        {
                            offsetY = GlobalSettings.Instance.multiNoteOffset;
                        }
                        else
                        {
                            offsetY = 0f;
                        }
                    }
                    addNote();
                    noteNum++;
                }

                if (GlobalSettings.Instance.notes[noteNum] == "end")
                {
                    GlobalSettings.Instance.sheetEnd = true;
                    noteNum = 1;
                    offsetY = 0;
                }
            }
            if (audioTrack.GetComponent<AudioSource>().isPlaying == false)
            {
                GlobalSettings.Instance.isStarted = false;
                if(GlobalSettings.Instance.sheetEnd == true)
                {
                    if(GlobalSettings.Instance.combo == GlobalSettings.Instance.noteSum)
                    {
                        if(GlobalSettings.Instance.combo == GlobalSettings.Instance.pureNum)
                        {
                            GlobalSettings.Instance.scoreLevel = "PURE MEMORY";
                            GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = "PURE MEMORY";
                        }
                        else
                        {
                            GlobalSettings.Instance.scoreLevel = "FULL RACALL";
                            GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = "FULL RACALL";
                        }
                    }
                    else
                    {
                        GlobalSettings.Instance.scoreLevel = "TRACK COMPLETE";
                        GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = "TRACK COMPLETE";
                    }
                    Invoke("showResult", 2f);
                }
            }
            else
            {
                GlobalSettings.Instance.isStarted = true;
            }

        }
    }

    private void addNote()
    {
        
        switch (noteDetail[2])
        {
            case "11":
                tapNote = notePool.Instance.GetObj(TapNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k1Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                tapNote.GetComponent<tapNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<tapNote>().keyToPress = GlobalSettings.Instance.key1;
                break;
            case "12":
                tapNote = notePool.Instance.GetObj(TapNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k2Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                tapNote.GetComponent<tapNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<tapNote>().keyToPress = GlobalSettings.Instance.key2;
                break;
            case "13":
                tapNote = notePool.Instance.GetObj(TapNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k3Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                tapNote.GetComponent<tapNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<tapNote>().keyToPress = GlobalSettings.Instance.key3;
                break;
            case "14":
                tapNote = notePool.Instance.GetObj(TapNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k4Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                tapNote.GetComponent<tapNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<tapNote>().keyToPress = GlobalSettings.Instance.key4;
                break;
            case "21":
                tapNote = notePool.Instance.GetObj(HoldNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k1Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                Vector3 oldTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localScale = new Vector3(oldTailScale.x, 2f * noteSpeed * noteScale * (float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])),oldTailScale.z);
                //Debug.Log((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 30f);
                tapNote.GetComponent<HoldNote>().holdCombo = (int)((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f);//该holdnote除了头判以外的物量
                if ((float)(tapNote.GetComponent<HoldNote>().holdCombo + 1) - ((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f) < 0.0011)//0.9989以上物量的算1物量 暂时用这种方式解决物量计算问题
                {
                    tapNote.GetComponent<HoldNote>().holdCombo++;
                }
                //Debug.Log(tapNote.GetComponent<HoldNote>().holdCombo);
                //tapNote.GetComponent<HoldNote>().holdTime = float.Parse(noteDetail[3]) - float.Parse(noteDetail[0]);
                //tapNote.GetComponent<HoldNote>().holdTimePerHit = 30f / float.Parse(noteDetail[1]);
                Vector3 oldTailPos = tapNote.transform.Find("HoldTail").transform.localPosition;
                Vector3 newTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localPosition = new Vector3(oldTailPos.x, newTailScale.y / 4f, oldTailPos.z);
                tapNote.GetComponent<HoldNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<HoldNote>().keyToPress = GlobalSettings.Instance.key1;
                break;
            case "22":
                tapNote = notePool.Instance.GetObj(HoldNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k2Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                oldTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localScale = new Vector3(oldTailScale.x, 2f * noteSpeed * noteScale * (float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])), oldTailScale.z);
                tapNote.GetComponent<HoldNote>().holdCombo = (int)((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f);//该holdnote除了头判以外的物量
                if ((float)(tapNote.GetComponent<HoldNote>().holdCombo + 1) - ((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f) < 0.0011)//0.9989以上物量的算1物量 暂时用这种方式解决物量计算问题
                {
                    tapNote.GetComponent<HoldNote>().holdCombo++;
                }
                //tapNote.GetComponent<HoldNote>().holdTime = float.Parse(noteDetail[3]) - float.Parse(noteDetail[0]);
                //tapNote.GetComponent<HoldNote>().holdTimePerHit = 30f / float.Parse(noteDetail[1]);
                oldTailPos = tapNote.transform.Find("HoldTail").transform.localPosition;
                newTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localPosition = new Vector3(oldTailPos.x, newTailScale.y / 4f, oldTailPos.z);
                tapNote.GetComponent<HoldNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<HoldNote>().keyToPress = GlobalSettings.Instance.key2;
                break;
            case "23":
                tapNote = notePool.Instance.GetObj(HoldNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k3Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                oldTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localScale = new Vector3(oldTailScale.x, 2f * noteSpeed * noteScale * (float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])), oldTailScale.z);
                tapNote.GetComponent<HoldNote>().holdCombo = (int)((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f);//该holdnote除了头判以外的物量
                if ((float)(tapNote.GetComponent<HoldNote>().holdCombo + 1) - ((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f) < 0.0011)//0.9989以上物量的算1物量 暂时用这种方式解决物量计算问题
                {
                    tapNote.GetComponent<HoldNote>().holdCombo++;
                }
                //tapNote.GetComponent<HoldNote>().holdTime = float.Parse(noteDetail[3]) - float.Parse(noteDetail[0]);
                //tapNote.GetComponent<HoldNote>().holdTimePerHit = 30f / float.Parse(noteDetail[1]);
                oldTailPos = tapNote.transform.Find("HoldTail").transform.localPosition;
                newTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localPosition = new Vector3(oldTailPos.x, newTailScale.y / 4f, oldTailPos.z);
                tapNote.GetComponent<HoldNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<HoldNote>().keyToPress = GlobalSettings.Instance.key3;
                break;
            case "24":
                tapNote = notePool.Instance.GetObj(HoldNote);
                tapNote.transform.position = new Vector3(GlobalSettings.Instance.k4Pos.x, GlobalSettings.Instance.topPosY - offsetY, 0f);
                oldTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localScale = new Vector3(oldTailScale.x, 2f * noteSpeed * noteScale * (float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])), oldTailScale.z);
                tapNote.GetComponent<HoldNote>().holdCombo = (int)((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f);//该holdnote除了头判以外的物量
                if ((float)(tapNote.GetComponent<HoldNote>().holdCombo + 1) - ((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f) < 0.0011) //0.9989以上物量的算1物量 暂时用这种方式解决物量计算问题
                {
                    tapNote.GetComponent<HoldNote>().holdCombo++;
                }
                //tapNote.GetComponent<HoldNote>().holdTime = float.Parse(noteDetail[3]) - float.Parse(noteDetail[0]);
                // tapNote.GetComponent<HoldNote>().holdTimePerHit = 30f / float.Parse(noteDetail[1]);
                oldTailPos = tapNote.transform.Find("HoldTail").transform.localPosition;
                newTailScale = tapNote.transform.Find("HoldTail").transform.localScale;
                tapNote.transform.Find("HoldTail").transform.localPosition = new Vector3(oldTailPos.x, newTailScale.y / 4f, oldTailPos.z);
                tapNote.GetComponent<HoldNote>().noteSpeed = noteSpeed;
                tapNote.GetComponent<HoldNote>().keyToPress = GlobalSettings.Instance.key4;
                break;
            default:
                Debug.Log("noteDetail error");
                break;
        }
    }
    public void lostHit()
    {
        GlobalSettings.Instance.combo = 0;
        GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = "";
        GlobalSettings.Instance.lostNum++;
    }
    public void farHit()
    {
        GlobalSettings.Instance.combo++;
        GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = GlobalSettings.Instance.combo.ToString();
        GlobalSettings.Instance.farNum++;
        GlobalSettings.Instance.score += GlobalSettings.Instance.farScore;
        GameObject.Find("Canvas/Score").GetComponent<Text>().text = "";
        scoreString = ((int)GlobalSettings.Instance.score).ToString();
        for (int i = 0; i < 8 - scoreString.Length; i++)  //将float的分数转化为整数int型之后转化为string从而获取分数位数以便在分数Text前面补0
        {
            GameObject.Find("Canvas/Score").GetComponent<Text>().text += "0";
        }
        GameObject.Find("Canvas/Score").GetComponent<Text>().text += scoreString;
    }

    public void pureHit()
    {
        GlobalSettings.Instance.combo++;
        GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = GlobalSettings.Instance.combo.ToString();
        GlobalSettings.Instance.pureNum++;
        if (GlobalSettings.Instance.pureNum == GlobalSettings.Instance.noteSum)
        {
            GlobalSettings.Instance.score += GlobalSettings.Instance.pureScore;
            GameObject.Find("Canvas/Score").GetComponent<Text>().text = "10000000";
        }
        else
        {
            GlobalSettings.Instance.score += GlobalSettings.Instance.pureScore;
            GameObject.Find("Canvas/Score").GetComponent<Text>().text = "";
            scoreString = ((int)GlobalSettings.Instance.score).ToString();
            for (int i = 0;i<8- scoreString.Length;i++)  //将float的分数转化为整数int型之后转化为string从而获取分数位数以便在分数Text前面补0
            {
                GameObject.Find("Canvas/Score").GetComponent<Text>().text += "0";
            }
            GameObject.Find("Canvas/Score").GetComponent<Text>().text += scoreString;
        }
    }

    private void showResult()
    {
        SceneManager.LoadScene(2);
    }    
}
